package com.hxkcloud.nurse.popup;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.beans.DrugBean;
import com.hxkcloud.nurse.utils.BrightnessUtils;
import com.hxkcloud.nurse.wheelview.WheelView;
import com.hxkcloud.nurse.wheelview.adapter.ArrayWheelAdapter;

import java.util.List;

/**
 * Created by william_sun on 2016-10-08.
 * Description:添加修改服药记录-剂量选择
 */
public class DoseSelectPopWindow extends PopupWindow implements View.OnClickListener {
    private Activity context;
    private WheelView wheel_doses;

    private Button bt_cancel;
    private Button bt_sure;

    private List<DrugBean.DrugDose> dose_list;
    private String doses[];

    private onDoseSelectListener listener;

    public DoseSelectPopWindow(Activity context, onDoseSelectListener listener) {
        this.context = context;
        this.listener = listener;
        View rootView = LayoutInflater.from(context).inflate(R.layout.popup_selectaddress, null);
        setContentView(rootView);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(0x00000000));

        setAnimationStyle(R.style.popupwindow_anim_default);
        initViews(rootView);
        initDatas();
        initListener();
    }

    private void initViews(View rootView) {
        wheel_doses = (WheelView) rootView.findViewById(R.id.wheel_selectaddress_add);
        bt_cancel = (Button) rootView.findViewById(R.id.bt_selectaddress_cancel);
        bt_sure = (Button) rootView.findViewById(R.id.bt_selectaddress_sure);

    }

    private void initDatas() {
        wheel_doses.setCyclic(false);
        wheel_doses.setVisibleItems(5);

    }

    private void initListener() {
        bt_sure.setOnClickListener(this);
        bt_cancel.setOnClickListener(this);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                BrightnessUtils.lightOn(context);
            }
        });
    }

    public void show(View parent, List<DrugBean.DrugDose> dose_list, String curId) {
        this.dose_list = dose_list;
        doses = new String[dose_list.size()];
        int curItem = 0;
        for (int i = 0; i < dose_list.size(); i++) {
            doses[i] = dose_list.get(i).getDoseName();
            if (!TextUtils.isEmpty(curId)) {
                if (dose_list.get(i).getDoseId().equals(curId)) {
                    curItem = i;
                }
            }
        }

        ArrayWheelAdapter adapter = new ArrayWheelAdapter(context, doses);
        wheel_doses.setViewAdapter(adapter);
        wheel_doses.setCurrentItem(curItem);
        showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        BrightnessUtils.lightOff(context);
    }

    @Override
    public void onClick(View v) {
        dismiss();
        switch (v.getId()) {
            case R.id.bt_selectaddress_cancel:

                break;
            case R.id.bt_selectaddress_sure:
                if (listener != null)
                    listener.onDoseSelect(wheel_doses.getCurrentItem());
                break;
        }
    }

    public interface onDoseSelectListener {
        void onDoseSelect(int position);
    }
}
